/*
 * Copyright 2010 David de Mingo
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.shalma.persistence;

import org.shalma.internal.reflection.IdGenerator;
import org.shalma.model.Entity;

public class Query<E extends Entity> {

	public static final String CLASS_PROPERTY = "C";

	private String property;
	private FilterOperator operator;

	private Type type;
	private String stringValue;
	private Long longValue;

	private String cursor;
	private int size = 20;

	private int offset;

	public enum FilterOperator {
		EQUAL, GREATER_THAN, GREATER_THAN_OR_EQUAL, LESS_THAN, LESS_THAN_OR_EQUAL
	}

	enum Type {
		LONG, STRING
	}

	public Query(String property, FilterOperator operator, String value) {

		this.property = property;
		this.operator = operator;
		type = Type.STRING;
		stringValue = value;
	}

	public Query(String property, FilterOperator operator, Long value) {

		this.property = property;
		this.operator = operator;
		type = Type.LONG;
		longValue = value;
	}

	protected Query() {
	}

	public String getProperty() {
		return property;
	}

	public FilterOperator getOperator() {
		return operator;
	}

	public Object getValue() {

		Object value = null;

		switch (type) {
		case LONG:
			value = longValue;
			break;
		case STRING:
			value = stringValue;
			break;
		}

		return value;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	@Override
	public String toString() {
		return "(" + property + " " + getValue() + ")";
	}
}
